package com.sie.demo.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sie.demo.entity.UserRole;
import com.sie.demo.entity.vo.SetUserRoleVo;
import com.sie.demo.mapper.UserRoleMapper;
import com.sie.demo.service.UserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author liaozhifeng
 * @date 2021/8/9 10:39
 * @Version 1.0
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper,UserRole> implements UserRoleService {


    @Resource
    UserRoleMapper userRoleMapper;
    @Override
    public List<Integer> selectRoleByUserId(Integer userId) {

        List<Integer> list = userRoleMapper.selectRoleByUserId(userId);
        return list;
    }

    @Override
    public Integer deleteByUserId(Integer userId) {
        return userRoleMapper.deleteByUserIdInteger(userId);
    }

    @Override
    public Integer addRoleByUserID(Integer userId,String[] roleSelected) {
        SetUserRoleVo setUserRoleVo = new SetUserRoleVo();
        //        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp  createTime =new java.sql.Timestamp (date.getTime());
//        将前端传来的值封装到对象中写入数据库
        setUserRoleVo.setCreateTime(createTime);
        List roleIds = Arrays.asList(roleSelected);
        setUserRoleVo.setRoleIds(roleIds);
        setUserRoleVo.setUserId(userId);
        return userRoleMapper.addRoleByUserID(setUserRoleVo);
    }
}
